#!/bin/bash

###
# Copyright 2009, Jethro Grassie, Creative Jar Technology Ltd (http://www.cjtech.co.uk)
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
###


function compile()
{
    echo "Compiling..."
    if [ ! -d build ]; then
        mkdir build
    fi

    javac -target 1.5 -source 1.5 \
        -cp /Library/Frameworks/JavaFX.framework/Versions/1.2/lib/desktop/javafx-common.jar:/Library/Frameworks/JavaFX.framework/Versions/1.2/lib/shared/javafxrt.jar \
        com/adobe/cairngorm/business/RMIRunnable.java \
        com/adobe/cairngorm/business/RMIListener.java \
        com/adobe/cairngorm/business/RMICallInfo.java \
        -d build

    if [[ $? != 0 ]]; then
        exit 1;
    fi

    cp com/cjt/rmi/server.policy build/
    cp com/cjt/rmi/client.policy build/

    cd build
    jar cf RMIService.jar \
        com/adobe/cairngorm/business/RMIRunnable.class \
        com/adobe/cairngorm/business/RMICallInfo.class \
        com/adobe/cairngorm/business/RMIListener.class
    cd -

    javafxc -target 1.5 -source 1.5 -cp build/RMIService.jar \
        com/cjt/sample/*.fx \
        com/cjt/sample/events/*.fx \
        com/cjt/sample/commands/*.fx \
        com/cjt/sample/business/*.fx \
        -d build

    if [[ $? != 0 ]]; then
        exit 1;
    fi

    cp com/adobe/cairngorm/CairngormError_en.fxproperties build/com/adobe/cairngorm/
}

function launch()
{
    echo "Launching..."
    if [ ! -d build ]; then
        echo "Not built!"
        exit 1;
    fi
    cd build
    BUILD_DIR=`pwd`
    set -x
    #javafx com.cjt.sample.Main
    javafx -Djava.rmi.server.codebase=file:$BUILD_DIR/TimeUtilCommon.jar \
        -Djava.security.policy=client.policy com.cjt.sample.Main
    set +x
    cd -
}

function dist()
{
    echo "Making dist..."
    if [ ! -d build/tmp ]; then
        mkdir -p build/tmp
    fi
    cp -R com build/tmp
    javafxpackager -sign -src build/tmp -d build/dist -p desktop -appName "CairngormFX Test" -appVendor "CJT Ltd" -appWidth 400 -appHeight 300 -appClass com.cjt.sample.Main
    rm -r build/tmp

#TODO: Edit html to include launch button...
#
# <script src="http://java.com/js/deployJava.js"></script>
# <p>
#     <script>
#         deployJava.createWebStartLaunchButton('CairngormFX Test.jnlp','1.5+');
#     </script>
# </p>
#
}

function rmitest()
{
    echo "Making RMI test apps"
    javac -target 1.5 -source 1.5 com/cjt/rmi/common/ITimeUtil.java -d build
    cd build
    jar cvf TimeUtilCommon.jar com/cjt/rmi/common/ITimeUtil.class
    cd -
    javac -target 1.5 -source 1.5 -cp build/TimeUtilCommon.jar com/cjt/rmi/server/TimeUtil.java -d build
    javac -target 1.5 -source 1.5 -cp build/TimeUtilCommon.jar com/cjt/rmi/client/TimeUtilClient.java -d build
    cp com/cjt/rmi/server.policy build/
    cp com/cjt/rmi/client.policy build/

#
# To test, cd to build.
#
# Start rmi regsitry:
#   rmiregistry &
# or
#    rmiregistry -J-Dsun.rmi.log.debug=true -J-Dsun.rmi.server.exceptionTrace=true -J-Dsun.rmi.loader.logLevel=verbose -J-Dsun.rmi.dgc.logLevel=verbose -J-Dsun.rmi.transport.logLevel=verbose -J-Dsun.rmi.transport.tcp.logLevel=verbose
#
# Start server:
#   java -cp .:TimeUtilCommon.jar -Djava.rmi.server.codebase=file:/Users/jethro/Projects/CairngormFX/git/build/TimeUtilCommon.jar -Djava.rmi.server.hostname=localhost -Djava.security.policy=server.policy com.cjt.rmi.server.TimeUtil 
#
# Run client:
#   java -cp .:TimeUtilCommon.jar -Djava.rmi.server.codebase=file:/Users/jethro/Projects/CairngormFX/git/build/TimeUtilCommon.jar -Djava.security.policy=client.policy com.cjt.rmi.client.TimeUtilClient localhost
#
}

while getopts "cldr" name
do
    case $name in
	c)
		compile
        exit;;
	l)
        launch
        exit;;
    d)
        dist
        exit;;
    r)
        rmitest
        exit;;
    esac
done
shift $(($OPTIND - 1))

# Here so default...
compile
launch

